package com.siqi.hr.system.common.authority;

import com.siqi.hr.spring.SEApplicationContext;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;

public class Sysdata implements Serializable {
	private static final long serialVersionUID = -7981059402867064760L;

	/**
	 * 数据库名称
	 */
	private String database;

	//单例模式
	private static Sysdata sysdata = null;
	public static Sysdata getInstance() {
		if (sysdata == null) {
			synchronized (Sysdata.class) {
				if (sysdata == null) {
					sysdata = new Sysdata();
				}
			}
		}
		return sysdata;
	}

	public String getDatabase() {
		return this.database;
	}

    /**
	 * 获取数据库名称方法
	 * @author         xiaogq
	 */
	public void loadData() {
		BasicDataSource bds = (BasicDataSource) SEApplicationContext.getBean("dataSource");
		String url = bds.getUrl().split("\\?")[0];
		String databaseName = url.substring(url.lastIndexOf("/") + 1);
		if (StringUtils.isNotEmpty(databaseName)){
			this.database = databaseName;
		}else {
			throw new RuntimeException("数据库连接异常，请检查数据库配置是否正确！");
		}
	}
}
